<?php

use Phalcon\Mvc\Model\Validator\Email as Email;

class ClientInfo extends \Phalcon\Mvc\Model
{

    public $latlng;
    
    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $name;

    /**
     *
     * @var string
     */
    public $about;

    /**
     *
     * @var string
     */
    public $city;

    /**
     *
     * @var string
     */
    public $location;

    /**
     *
     * @var string
     */
    public $contact_no;

    /**
     *
     * @var string
     */
    public $email;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     *
     * @var string
     */
    public $date_modified;

    
    public $profile_pic;

    public $views;

    public $status;

    public $noofworkers;
    public $admin_id;
    public function beforeValidationOnUpdate()
    {
        $this->date_modified = CURR_DATE;
    }
    
    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'name' => 'name', 
            'about' => 'about', 
            'city' => 'city', 
            'location' => 'location', 
            'contact_no' => 'contact_no', 
            'email' => 'email', 
            'date_created' => 'date_created', 
            'date_modified' => 'date_modified',
            'profile_pic' => 'profile_pic',
            'views' => 'views',
            'status' => 'status',
            'noofworkers' => 'noofworkers',
            'admin_id' => 'admin_id',
            "latlng"=>"latlng"
        );
    }

    public function beforeValidationOnCreate(){
        $this->status = 0;
        $this->date_created = CURR_DATE;
        if(!$this->profile_pic) $this->profile_pic = '1422600100/no-dp_wudgdv.jpg';
    }
    public function listNames()
    {
        $phql = "SELECT name,profile_pic,id FROM ClientInfo";
        $data = $this->modelsManager->executeQuery($phql);
        return $data;
    }
    public function listCompany($status)
    {
        $phql = "SELECT 
                 CONCAT(uat.fname,' ',uat.lname) as admin_fullname,ci.date_modified,ci.date_created,ci.name,ci.id as company_id,
                 ROUND(SUM(cr.rate) / (4 * COUNT(cr.id))) as stars,ci.noofworkers
                 FROM ClientInfo ci
                 LEFT JOIN CompanyRatings cr ON cr.company_id = ci.id
                 LEFT JOIN UserAccountTb uat ON uat.id = ci.admin_id
                 WHERE status = ?0
                 GROUP BY ci.id
                 ORDER BY ci.date_created DESC";
        return $this->modelsManager->executeQuery($phql,array(0=>$status));
    }

}
